package com.tianhui.consignortwofei.mvp.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fgs.common.imageView.strategy.ImageLoader;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.DisplayPictureInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.role.RoleController;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

/**
 * @author fgs
 * @date 2020/8/28
 * @description
 **/
public class DisplayPictureAdapter extends RecyclerArrayAdapter<DisplayPictureInfo> {

    //是否需要显示编辑
    private boolean isNeedEdit = true;

    public void setNeedEdit(boolean needEdit) {
        isNeedEdit = needEdit;
    }

    public DisplayPictureAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new DisplayPictureViewHolder(parent, R.layout.item_display_picture);
    }

    private class DisplayPictureViewHolder extends BaseViewHolder<DisplayPictureInfo> {

        private final TextView titleTextView;
        private final ImageView imageView;
        private final ImageView editImageView;

        public DisplayPictureViewHolder(ViewGroup parent, int res) {
            super(parent, res);
            titleTextView = getView(R.id.item_display_picture_titleTextView);
            imageView = getView(R.id.item_display_picture_imageView);
            editImageView = getView(R.id.item_display_picture_editImageView);

            addOnClickListener(R.id.item_display_picture_editImageView);
        }

        @Override
        public void setData(DisplayPictureInfo data) {
            super.setData(data);
            if (!TextUtils.isEmpty(data.title)) {
                titleTextView.setVisibility(View.VISIBLE);
            } else {
                titleTextView.setVisibility(View.GONE);
            }

            titleTextView.setText(data.title);
            if (!TextUtils.isEmpty(data.imageId)) {
                if (data.imageId.contains(",")) { //若有多张图片，只显示第一张
                    String[] strings = data.imageId.split(",");
                    ImageLoader.init().loadImageWithId(imageView, strings[0]);
                } else {
                    ImageLoader.init().loadImageWithId(imageView, data.imageId);
                }
            }

            boolean isShowEditImage = RoleController.getInstance().isShowEditImage();
            if (isShowEditImage) {
                OrderInfo orderInfo = data.orderInfo;
                //司机完成交货且当前角色可以显示
                if (orderInfo != null && (orderInfo.isstate == 9 || orderInfo.isstate >= 12) && isNeedEdit) {
                    editImageView.setVisibility(View.VISIBLE);
                } else {
                    editImageView.setVisibility(View.GONE);
                }
            } else {
                editImageView.setVisibility(View.GONE);
            }


        }
    }
}
